import type { FormState } from '@/common/types/user-type'
import type { Rule } from 'ant-design-vue/es/form'

// 手机号码规则校验
export const phoneRules: Rule[] = [
  { required: true, message: '请输入手机号码', trigger: 'change' },
  {
    pattern: /^1[3-9]\d{9}$/,
    message: '手机号码必须以1开头,并且满11位的号码',
    trigger: 'blur',
  },
]

// 密码校验规则
export const passwordRules: Rule[] = [
  { required: true, message: '请输入密码', trigger: 'change' },
  { min: 8, message: '密码至少为 8 位字符', trigger: 'blur' },
  {
    pattern: /^[^, ]+$/,
    message: '密码不允许包含空格或逗号',
    trigger: 'blur',
  },
]

// 第二次密码校验规则
export const getRepasswordRules = (formState: FormState): Rule[] => [
  { required: true, message: '请输入密码', trigger: 'change' },
  { min: 8, message: '密码至少为 8 位字符', trigger: 'blur' },
  {
    pattern: /^[^, ]+$/,
    message: '密码不允许包含空格和逗号',
    trigger: 'blur',
  },
  {
    validator: (rule, value, callback) => {
      if (value !== formState.password) {
        return Promise.reject('两次输入的密码不一致')
      } else {
        return Promise.resolve()
      }
    },
    trigger: 'blur',
  },
]

// 验证码校验规则
export const verificationRules : Rule[] =[
  { required: true, message: '验证码为空', trigger: 'change' },
  { min: 4, max: 6, message: '验证码应为 6 位数字', trigger: 'blur' },
]

// 邮箱账号验证规则
export const mailboxRules: Rule[] = [
  { required: true, message: '请输入邮箱账号', trigger: 'change' },
  {
    pattern: /^[0-9a-zA-Z_-]+@[0-9a-zA-Z_-]+\.(com|cn|net|vip|cloud)$/,
    message: '请检查账号是否正确，结尾以@qq.com',
    trigger: 'blur',
  },
]

// 用户协议验证规则
export const rememberRules: Rule[] = [
  {
    type: 'array',
    required: true,
    message: '请勾选用户协议',
    trigger: 'change',
  },
]

export const getRules = (formState: FormState) => ({
  phoneNumber: phoneRules, // 手机号码
  email: mailboxRules, //  邮箱账号
  password: passwordRules, //  邮箱密码
  repassword: getRepasswordRules(formState), // 二次验证密码
  code:verificationRules, // 验证码
  remember: rememberRules, // 用户协议
})
